package com.cesce.batch.cobros.reverse;

import java.math.BigDecimal;

import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Transient;
import javax.persistence.TypedQuery;

import org.springframework.roo.addon.dbre.RooDbManaged;
import org.springframework.roo.addon.entity.RooEntity;
import org.springframework.roo.addon.javabean.RooJavaBean;
import org.springframework.roo.addon.tostring.RooToString;

@RooJavaBean
@RooToString
@RooEntity(versionField = "", table = "COBCOMUNICADOS", schema = "COBROS")
@RooDbManaged(automaticallyDelete = false)
public class Cobcomunicados implements Comparable<Cobcomunicados>{
	
	
    @Id
//    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator="COMUNICADOS_GEN")
//    @SequenceGenerator(name="COMUNICADOS_GEN", sequenceName="SEQ_COBCOMUNICADOS", allocationSize=1)
//    
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "IDCOMUNICADO")
    private BigDecimal idcomunicado;
    
	@Transient	
	private String divisaString;
	
    @Transient
    private String situacion;
    
	
	public BigDecimal getDivisaOutput() {
		if(getDivisa()==null || String.valueOf(getDivisa().intValue()).length()>3) {
			return new BigDecimal(0);
		} else return getDivisa();		
	}
	
	public String getCodigocesceOutput() {	
	
		StringBuffer aux = new StringBuffer(getCodigocesce());
		
		if(getCodigocesce()==null || getCodigocesce().isEmpty() || getCodigocesce().length() > 9) {
			aux=new StringBuffer("000000000");		
		}		
		
		for(int i=aux.length();i<9; i++) {
			aux.insert(0, "0");			
		}
				
		return aux.toString();
	}
	
	public String getSptoOutput() {
		
		StringBuffer aux = new StringBuffer(getSpto());
		
		if(getSpto()==null || getSpto().isEmpty() || getSpto().length()>6) {
			aux=new StringBuffer("000000");		
		}		
		
		for(int i=aux.length();i<6; i++) {
			aux.insert(0, "0");			
		}
				
		return aux.toString();
	}	

	public Integer getValorDecimalImporte() {		
		Integer decimals = new Integer(0);
		try {
			decimals=Integer.parseInt(getValorimporte().setScale(3, BigDecimal.ROUND_HALF_UP).toString().split("\\.")[1]);
		} catch(Throwable e) {
			
		}
		
		return decimals;
	}
	
	public BigDecimal getValorimporteOutput() {		

		return getValorimporte()!=null?getValorimporte().subtract(new BigDecimal(0.5)):null; 
	}	
	
	
    public static BigDecimal countCobcomunicadosesByRegistroAndEstado(BigDecimal idregistro, BigDecimal idestado) {
    	TypedQuery<Long> query = entityManager().createQuery("SELECT COUNT(o) FROM Cobcomunicados o WHERE o.idregistro.idregistro=:idregistro and o.idestado.idestado=:idestado", Long.class);
    	query.setParameter("idregistro", idregistro);
    	query.setParameter("idestado", idestado);
    	return new BigDecimal(query.getSingleResult());
    }
    
	
    public static BigDecimal countCobcomunicadosesByRegistro(BigDecimal idregistro) {
    	TypedQuery<Long> query = entityManager().createQuery("SELECT COUNT(o) FROM Cobcomunicados o WHERE o.idregistro.idregistro=:idregistro", Long.class);
    	query.setParameter("idregistro", idregistro);
    	return new BigDecimal(query.getSingleResult());
    }
    
    public static BigDecimal sumCobcomunicadosesByRegistroAndEstado(BigDecimal idregistro, BigDecimal idestado) {
    	TypedQuery<Long> query = entityManager().createQuery("SELECT SUM(o.valorimporte) FROM Cobcomunicados o WHERE o.idregistro.idregistro=:idregistro and o.idestado.idestado=:idestado", Long.class);
    	query.setParameter("idregistro", idregistro);
    	query.setParameter("idestado", idestado);
    	return new BigDecimal(query.getSingleResult());
    }

	@Override
	public int compareTo(Cobcomunicados o) {
		int result=0;
		
		if(Integer.parseInt(this.getLineaoriginal())<Integer.parseInt(o.getLineaoriginal())) result=-1;
		else if(Integer.parseInt(this.getLineaoriginal())>Integer.parseInt(o.getLineaoriginal())) result=1;
		
		return result;
	}

	
}
